package com.gable.exhibition.controller;

import com.alibaba.fastjson.JSON;
import com.gable.common.util.CacheData;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.StringUtil;
import com.gable.exhibition.model.User;
import com.gable.exhibition.service.PartService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.PartView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Lesson on 2018/3/2.
 */
@Controller
@RequestMapping(value="/part")
public class PartController {

        Logger logger = Logger.getLogger(this.getClass());


        @Autowired
        PartService partService;

    /**
     * 批量操作备件信息
     * @param request
     * @param response
     * @return
     */
        @RequestMapping(value="/savePartByBatch",method = RequestMethod.POST, produces="application/json;charset=utf-8")
        @ResponseBody
        public ReturnResultUtil savePartByBatch(HttpServletRequest request , HttpServletResponse response){
            try{

                Long userId = null;
                if(!StringUtil.isEmpty(request.getParameter("userId"))){
                    userId = Long.parseLong(request.getParameter("userId"));
                }
                User user = CacheData.getSessionUser(userId,1);

                if(StringUtil.isEmpty(request.getParameter("keyId"))){
                    return new ReturnResultUtil(500, MessageUtil.PARTTKEYID);
                }

                if(StringUtil.isEmpty(request.getParameter("keyType"))){
                    return new ReturnResultUtil(500,MessageUtil.PARTKEYTYPE);
                }

                if(StringUtil.isEmpty(request.getParameter("partList"))){
                    return new ReturnResultUtil(500,MessageUtil.SAVEEXCEPTION);
                }


                Long keyId = Long.parseLong(request.getParameter("keyId"));
                Long keyType = Long.parseLong(request.getParameter("keyType"));

                String partList = request.getParameter("partList");
                List<PartView> partViewList = JSON.parseArray(partList,PartView.class);

                ReturnResultUtil rru = partService.savePartByBatch(partViewList, user,keyId,keyType);

                return rru;

            }catch (Exception e){
                e.printStackTrace();
            }
            return new ReturnResultUtil(500,MessageUtil.SAVEEXCEPTION);
        }

    @RequestMapping(value="/queryPart",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryPart(HttpServletRequest request ,PartView partView){
            ReturnResultUtil resultUtil = new ReturnResultUtil(500,MessageUtil.QUERYEXCEPTION);
        try{
            Long keyId = partView.getKeyId();
            Long keyType = partView.getKeyType();

            if(keyId == null)
                return new ReturnResultUtil(500,MessageUtil.KEYIDISNULL);
            if(keyType == null)
                return new ReturnResultUtil(500,MessageUtil.KEYTYPEISNULL);

            resultUtil = partService.queryPart(partView);
            logger.debug(JSON.toJSONString(resultUtil));
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultUtil;
    }
}
